#include "stdafx.h"
#include "GarbageCollectorInvoker.h"
#include <fstream>

struct ForceGCThreadInfo
{
	int timeInterval;
	ICorProfilerInfo *pCorProfilerInfo;
};

unsigned int __stdcall CallForceGC(LPVOID ptr)
{
	ForceGCThreadInfo *pForceGCThreadInfo = (ForceGCThreadInfo *)(ptr);

	std::ofstream out("E:\\GCLog.log");

	while (true)
	{
		HRESULT hr = pForceGCThreadInfo->pCorProfilerInfo->ForceGC();
		out << hr << std::endl;
		out.flush();
		Sleep(pForceGCThreadInfo->timeInterval);
	}

	delete pForceGCThreadInfo;
	return 0;
}

void GarbageCollectorInvoker::DoGCEvery(int milliseconds)
{
	ForceGCThreadInfo *pForceGCThreadInfo = new ForceGCThreadInfo();
	pForceGCThreadInfo->pCorProfilerInfo = m_pCorProfilerInfo;
	pForceGCThreadInfo->timeInterval = milliseconds;

	_beginthreadex(NULL, NULL, CallForceGC, pForceGCThreadInfo, NULL, NULL);
}